package com.example.rdcproject.ving.notes.view.User;

import com.example.rdcproject.ving.notes.entity.User;
import com.example.rdcproject.ving.notes.service.ConnectionPool;
import com.example.rdcproject.ving.notes.util.NumberInputVerifier;
import com.example.rdcproject.ving.notes.util.UserSearchUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class SelectOneView extends JFrame {

    ConnectionPool connectionPool = ConnectionPool.getInstance();
    UserSearchUtil userSearchUtil = new UserSearchUtil();

    public SelectOneView() {

        JPanel mainPanel = new JPanel();
        add(mainPanel, BorderLayout.CENTER);

        JButton nameButton = new JButton("按昵称搜索");
        JButton countButton = new JButton("按单词学习总数搜索");
        JButton goalButton = new JButton("按每日计划数搜索");
        JButton continousButton = new JButton("按连续打卡天数搜索");


        //设置组件大小及位置
        nameButton.setBounds(280, 20, 80, 30);
        countButton.setBounds(20, 60, 80, 30);
        goalButton.setBounds(120, 60, 80, 30);
        continousButton.setBounds(220, 60, 80, 30);

        mainPanel.add(nameButton);
        mainPanel.add(countButton);
        mainPanel.add(goalButton);
        mainPanel.add(continousButton);



        //页面设置
        setTitle("搜索用户信息");
        setLayout(new CardLayout());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 350);

        nameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                remove(mainPanel);
                add(serachName());
            }
        });

        countButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                remove(mainPanel);
                add(serachCount());
            }
        });

        goalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                remove(mainPanel);
                add(searchGoal());
            }
        });

        continousButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                remove(mainPanel);
                add(serachContinous());
            }
        });

    }



    public JPanel serachName(){
        String name = JOptionPane.showInputDialog("请输入要查询的用户昵称");

        JPanel searchPanel = new JPanel();

        //创建表格查看搜索用户信息
        JTable table = new JTable();
        JScrollPane scrollPane = new JScrollPane(table);
        searchPanel.add(scrollPane, BorderLayout.CENTER);

        //创建一个motel
        String[] columnNames = { "用户名","单词记忆总数", "连续打卡天数","学习总时长","每日计划"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        table.setModel(model);


        List<User> userList = userSearchUtil.searchName(name);
        for (User user : userList) {
            if (user.isPrivate() == false) {
                Object[] row = {user.getNickname(), user.getWordCount(),
                        user.getContinuousDays(),user.getTotalStudyTime(),user.getDailyGoal()};
                model.addRow(row);
            }

        }
        return searchPanel;
    }

    public JPanel serachCount(){
        int count = Integer.parseInt(JOptionPane.showInputDialog("请输入要查询的用户单词记忆总数"));

        JPanel countPanel = new JPanel();

        //创建表格查看搜索用户信息
        JTable table = new JTable();
        JScrollPane scrollPane = new JScrollPane(table);
        countPanel.add(scrollPane, BorderLayout.CENTER);

        //创建一个motel
        String[] columnNames = { "用户名","连续打卡天数","学习总时长","每日计划"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        table.setModel(model);


        List<User> userList = userSearchUtil.searchCount(count);
        for (User user : userList) {
            if (user.isPrivate() == false) {
                Object[] row = {user.getNickname(), user.getContinuousDays(),user.getTotalStudyTime(),user.getDailyGoal()};
                model.addRow(row);
            }

        }
        return countPanel;
    }


    public JPanel searchGoal(){
        int goal = Integer.parseInt(JOptionPane.showInputDialog("请输入要查询的用户单词计划数"));


        JPanel goalPanel = new JPanel();

        //创建表格查看搜索用户信息
        JTable table = new JTable();
        JScrollPane scrollPane = new JScrollPane(table);

        goalPanel.add(scrollPane, BorderLayout.CENTER);


        //创建一个motel
        String[] columnNames = { "用户名","单词记忆总数", "连续打卡天数","学习总时长"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        table.setModel(model);


        List<User> userList = userSearchUtil.searchGoal(goal);
        for (User user : userList) {
            if (user.isPrivate() == false) {
                Object[] row = {user.getNickname(), user.getWordCount(),
                        user.getContinuousDays(),user.getTotalStudyTime()};
                model.addRow(row);
            }

        }
        return goalPanel;
    }

    public JPanel serachContinous(){
        int continous = Integer.parseInt(JOptionPane.showInputDialog("请输入要查询的用户连续打卡天数"));

        JPanel continousPanel = new JPanel();

        //创建表格查看搜索用户信息
        JTable table = new JTable();
        JScrollPane scrollPane = new JScrollPane(table);
        continousPanel.add(scrollPane, BorderLayout.CENTER);

        //创建一个motel
        String[] columnNames = { "用户名","单词记忆总数","学习总时长","每日计划"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        table.setModel(model);


        List<User> userList = userSearchUtil.searchContinous(continous);
        for (User user : userList) {
            if (user.isPrivate() == false) {
                Object[] row = {user.getNickname(), user.getWordCount(), user.getTotalStudyTime(),user.getDailyGoal()};
                model.addRow(row);
            }

        }
        return continousPanel;
    }
    
    
    

}
